package leecode.栈与队列问题;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author wangxi03 created on 2021/6/7 7:12 下午
 * @version v1.0
 *
 * https://leetcode-cn.com/problems/implement-stack-using-queues/
 *
 * 队列实现栈, 利用一个队列即可实现
 *
 * 请你仅使用两个队列实现一个后入先出（LIFO）的栈，并支持普通队列的全部四种操作（push、top、pop 和 empty）。
 */
public class MyStack {

    Queue<Integer> queue = null;

    /** Initialize your data structure here. */
    public MyStack() {
        queue = new LinkedList<>();
    }

    /** Push element x onto stack. */
    public void push(int x) {
        int size = queue.size();
        queue.offer(x);
        for (int i = 0; i < size; i++) {
            queue.offer(queue.poll());
        }
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue.poll();
    }

    /** Get the top element. */
    public int top() {
        return queue.peek();
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue.isEmpty();
    }
}
